package com.itheima.web.controller;

import com.itheima.domain.User;
import com.itheima.service.impl.ServiceImpl;
import com.itheima.utils.BeanUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/store/user")
public class UserServlet  extends HttpServlet{
     ServiceImpl service =new ServiceImpl();
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String operation = request.getParameter("operation");
        if("list".equals(operation)){
            this.list(request,response);
        }else if("toAdd".equals(operation)){
            this.toAdd(request,response);
        }else if("add".equals(operation)){
            this.add(request,response);
        }else if("delete".equals(operation)){
            this.delete(request,response);
        }else if("toEdit".equals(operation)){
            this.toEdit(request,response);
        }else if("edit".equals(operation)){
            this.edit(request,response);
        }
    }

    private void edit(HttpServletRequest request, HttpServletResponse response) throws IOException {
        User user = BeanUtil.fillBean(request, User.class);
        service.update(user);
        response.sendRedirect(request.getContextPath()+"/store/user?operation=list");

    }

    private void toEdit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id = request.getParameter("id");
        request.setAttribute("id",id);
        request.getRequestDispatcher("/WEB-INF/pages/User_Update.jsp").forward(request,response);
    }

    private void delete(HttpServletRequest request, HttpServletResponse response) throws IOException {
        User user = BeanUtil.fillBean(request, User.class);
        service.delete(user);
        response.sendRedirect(request.getContextPath()+"/store/user?operation=list");
    }

    private void add(HttpServletRequest request, HttpServletResponse response) throws IOException {
        User user = BeanUtil.fillBean(request, User.class);
        service.add(user);
        response.sendRedirect(request.getContextPath()+"/store/user?operation=list");

    }

    private void toAdd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getRequestDispatcher("/WEB-INF/pages/User_Add.jsp").forward(request,response);
    }


    private void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<User> all = service.findAll();
        request.setAttribute("page",all);
        request.getRequestDispatcher("/WEB-INF/pages/User_List.jsp").forward(request,response);
    }
}
